home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / phpMyAdmin / libraries / dbi / mysql.dbi.lib.php next >
Encoding:
PHP Script  |  2007-12-20  |  11.5 KB  |  393 lines

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4.  * Interface to the classic MySQL extension
  5.  *
  6.  * @version $Id: mysql.dbi.lib.php 10356 2007-05-08 20:39:33Z cybot_tm $
  7.  */
  8.  
  9. /**
  10.  *
  11.  */
  12. // MySQL client API
  13. if (!defined('PMA_MYSQL_CLIENT_API')) {
  14.     if (function_exists('mysql_get_client_info')) {
  15.         $client_api = explode('.', mysql_get_client_info());
  16.         define('PMA_MYSQL_CLIENT_API', (int)sprintf('%d%02d%02d', $client_api[0], $client_api[1], intval($client_api[2])));
  17.         unset($client_api);
  18.     } else {
  19.         define('PMA_MYSQL_CLIENT_API', 32332); // always expect the worst...
  20.     }
  21. }
  22.  
  23. function PMA_DBI_real_connect($server, $user, $password, $client_flags) {
  24.     global $cfg;
  25.  
  26.     if (empty($client_flags)) {
  27.         if ($cfg['PersistentConnections']) {
  28.             $link = @mysql_pconnect($server, $user, $password);
  29.         } else {
  30.             $link = @mysql_connect($server, $user, $password);
  31.         }
  32.     } else {
  33.         if ($cfg['PersistentConnections']) {
  34.             $link = @mysql_pconnect($server, $user, $password, $client_flags);
  35.         } else {
  36.             $link = @mysql_connect($server, $user, $password, FALSE, $client_flags);
  37.         }
  38.     }
  39.  
  40.     return $link;
  41. }
  42.  
  43. function PMA_DBI_connect($user, $password, $is_controluser = FALSE) {
  44.     global $cfg, $php_errormsg;
  45.  
  46.     $server_port   = (empty($cfg['Server']['port']))
  47.                    ? ''
  48.                    : ':' . $cfg['Server']['port'];
  49.  
  50.     if (strtolower($cfg['Server']['connect_type']) == 'tcp') {
  51.         $cfg['Server']['socket'] = '';
  52.     }
  53.  
  54.     $server_socket = (empty($cfg['Server']['socket']))
  55.                    ? ''
  56.                    : ':' . $cfg['Server']['socket'];
  57.  
  58.     $client_flags = 0;
  59.  
  60.     if (PMA_PHP_INT_VERSION >= 40300 && PMA_MYSQL_CLIENT_API >= 32349) {
  61.         // always use CLIENT_LOCAL_FILES as defined in mysql_com.h
  62.         // for the case where the client library was not compiled
  63.         // with --enable-local-infile
  64.         $client_flags |= 128;
  65.     }
  66.  
  67.     /* Optionally compress connection */
  68.     if (defined('MYSQL_CLIENT_COMPRESS') && $cfg['Server']['compress']) {
  69.         $client_flags |= MYSQL_CLIENT_COMPRESS;
  70.     }
  71.  
  72.     /* Optionally enable SSL */
  73.     if (defined('MYSQL_CLIENT_SSL') && $cfg['Server']['ssl']) {
  74.         $client_flags |= MYSQL_CLIENT_SSL;
  75.     }
  76.  
  77.     $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, $password, empty($client_flags) ? NULL : $client_flags);
  78.  
  79.     // Retry with empty password if we're allowed to
  80.     if (empty($link) && $cfg['Server']['nopassword'] && !$is_controluser) {
  81.         $link = PMA_DBI_real_connect($cfg['Server']['host'] . $server_port . $server_socket, $user, '', empty($client_flags) ? NULL : $client_flags);
  82.     }
  83.  
  84.     if (empty($link) && ! $is_controluser) {
  85.         if ($is_controluser) {
  86.             if (! defined('PMA_DBI_CONNECT_FAILED_CONTROLUSER')) {
  87.                 define('PMA_DBI_CONNECT_FAILED_CONTROLUSER', true);
  88.             }
  89.             return false;
  90.         }
  91.         PMA_auth_fails();
  92.     } // end if
  93.  
  94.     PMA_DBI_postConnect($link, $is_controluser);
  95.  
  96.     return $link;
  97. }
  98.  
  99. function PMA_DBI_select_db($dbname, $link = null) {
  100.     if (empty($link)) {
  101.         if (isset($GLOBALS['userlink'])) {
  102.             $link = $GLOBALS['userlink'];
  103.         } else {
  104.             return FALSE;
  105.         }
  106.     }
  107.     if (PMA_MYSQL_INT_VERSION < 40100) {
  108.         $dbname = PMA_convert_charset($dbname);
  109.     }
  110.     return mysql_select_db($dbname, $link);
  111. }
  112.  
  113. function PMA_DBI_try_query($query, $link = null, $options = 0) {
  114.     if (empty($link)) {
  115.         if (isset($GLOBALS['userlink'])) {
  116.             $link = $GLOBALS['userlink'];
  117.         } else {
  118.             return FALSE;
  119.         }
  120.     }
  121.     if (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION < 40100) {
  122.         $query = PMA_convert_charset($query);
  123.     }
  124.     if ($options == ($options | PMA_DBI_QUERY_STORE)) {
  125.         return @mysql_query($query, $link);
  126.     } elseif ($options == ($options | PMA_DBI_QUERY_UNBUFFERED)) {
  127.         return @mysql_unbuffered_query($query, $link);
  128.     } else {
  129.         return @mysql_query($query, $link);
  130.     }
  131. }
  132.  
  133. // The following function is meant for internal use only.
  134. // Do not call it from outside this library!
  135. function PMA_mysql_fetch_array($result, $type = FALSE) {
  136.     global $cfg, $allow_recoding, $charset, $convcharset;
  137.  
  138.     if ($type != FALSE) {
  139.         $data = mysql_fetch_array($result, $type);
  140.     } else {
  141.         $data = mysql_fetch_array($result);
  142.     }
  143.  
  144.     /* No data returned => do not touch it */
  145.     if (! $data) {
  146.         return $data;
  147.     }
  148.  
  149.     if (!defined('PMA_MYSQL_INT_VERSION') || PMA_MYSQL_INT_VERSION >= 40100
  150.         || !(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
  151.         /* No recoding -> return data as we got them */
  152.         return $data;
  153.     } else {
  154.         $ret = array();
  155.         $num = mysql_num_fields($result);
  156.         $i = 0;
  157.         for ($i = 0; $i < $num; $i++) {
  158.             $name = mysql_field_name($result, $i);
  159.             $flags = mysql_field_flags($result, $i);
  160.             /* Field is BINARY (either marked manually, or it is BLOB) => do not convert it */
  161.             if (stristr($flags, 'BINARY')) {
  162.                 if (isset($data[$i])) {
  163.                     $ret[$i] = $data[$i];
  164.                 }
  165.                 if (isset($data[$name])) {
  166.                     $ret[PMA_convert_display_charset($name)] = $data[$name];
  167.                 }
  168.             } else {
  169.                 if (isset($data[$i])) {
  170.                     $ret[$i] = PMA_convert_display_charset($data[$i]);
  171.                 }
  172.                 if (isset($data[$name])) {
  173.                     $ret[PMA_convert_display_charset($name)] = PMA_convert_display_charset($data[$name]);
  174.                 }
  175.             }
  176.         }
  177.         return $ret;
  178.     }
  179. }
  180.  
  181. function PMA_DBI_fetch_array($result) {
  182.     return PMA_mysql_fetch_array($result);
  183. }
  184.  
  185. function PMA_DBI_fetch_assoc($result) {
  186.     return PMA_mysql_fetch_array($result, MYSQL_ASSOC);
  187. }
  188.  
  189. function PMA_DBI_fetch_row($result) {
  190.     return PMA_mysql_fetch_array($result, MYSQL_NUM);
  191. }
  192.  
  193. /**
  194.  * Frees the memory associated with the results
  195.  *
  196.  * @param result    $result,...     one or more mysql result resources
  197.  */
  198. function PMA_DBI_free_result() {
  199.     foreach (func_get_args() as $result) {
  200.         if (is_resource($result)
  201.          && get_resource_type($result) === 'mysql result') {
  202.             mysql_free_result($result);
  203.         }
  204.     }
  205. }
  206.  
  207. /**
  208.  * Returns a string representing the type of connection used
  209.  * @uses    mysql_get_host_info()
  210.  * @uses    $GLOBALS['userlink']    as default for $link
  211.  * @param   resource        $link   mysql link
  212.  * @return  string          type of connection used
  213.  */
  214. function PMA_DBI_get_host_info($link = null)
  215. {
  216.     if (null === $link) {
  217.         if (isset($GLOBALS['userlink'])) {
  218.             $link = $GLOBALS['userlink'];
  219.         } else {
  220.             return false;
  221.         }
  222.     }
  223.     return mysql_get_host_info($link);
  224. }
  225.  
  226. /**
  227.  * Returns the version of the MySQL protocol used
  228.  * @uses    mysql_get_proto_info()
  229.  * @uses    $GLOBALS['userlink']    as default for $link
  230.  * @param   resource        $link   mysql link
  231.  * @return  integer         version of the MySQL protocol used
  232.  */
  233. function PMA_DBI_get_proto_info($link = null)
  234. {
  235.     if (null === $link) {
  236.         if (isset($GLOBALS['userlink'])) {
  237.             $link = $GLOBALS['userlink'];
  238.         } else {
  239.             return false;
  240.         }
  241.     }
  242.     return mysql_get_proto_info($link);
  243. }
  244.  
  245. /**
  246.  * returns a string that represents the client library version
  247.  * @uses    mysql_get_client_info()
  248.  * @return  string          MySQL client library version
  249.  */
  250. function PMA_DBI_get_client_info() {
  251.     return mysql_get_client_info();
  252. }
  253.  
  254. /**
  255.  * returns last error message or false if no errors occured
  256.  *
  257.  * @uses    PMA_MYSQL_INT_VERSION
  258.  * @uses    PMA_convert_display_charset()
  259.  * @uses    PMA_DBI_convert_message()
  260.  * @uses    $GLOBALS['errno']
  261.  * @uses    $GLOBALS['userlink']
  262.  * @uses    $GLOBALS['strServerNotResponding']
  263.  * @uses    $GLOBALS['strSocketProblem']
  264.  * @uses    mysql_errno()
  265.  * @uses    mysql_error()
  266.  * @uses    defined()
  267.  * @param   resource        $link   mysql link
  268.  * @return  string|boolean  $error or false
  269.  */
  270. function PMA_DBI_getError($link = null)
  271. {
  272.     $GLOBALS['errno'] = 0;
  273.     if (null === $link && isset($GLOBALS['userlink'])) {
  274.         $link =& $GLOBALS['userlink'];
  275.  
  276. // Do not stop now. On the initial connection, we don't have a $link,
  277. // we don't have a $GLOBALS['userlink'], but we can catch the error code
  278. //    } else {
  279. //            return FALSE;
  280.     }
  281.  
  282.     if (null !== $link) {
  283.         $error_number = mysql_errno($link);
  284.         $error_message = mysql_error($link);
  285.     } else {
  286.         $error_number = mysql_errno();
  287.         $error_message = mysql_error();
  288.     }
  289.     if (0 == $error_number) {
  290.         return false;
  291.     }
  292.  
  293.     // keep the error number for further check after the call to PMA_DBI_getError()
  294.     $GLOBALS['errno'] = $error_number;
  295.  
  296.     if (! empty($error_message)) {
  297.         $error_message = PMA_DBI_convert_message($error_message);
  298.     }
  299.  
  300.     // Some errors messages cannot be obtained by mysql_error()
  301.     if ($error_number == 2002) {
  302.         $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'] . ' ' . $GLOBALS['strSocketProblem'];
  303.     } elseif ($error_number == 2003) {
  304.         $error = '#' . ((string) $error_number) . ' - ' . $GLOBALS['strServerNotResponding'];
  305.     } elseif (defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION >= 40100) {
  306.         $error = '#' . ((string) $error_number) . ' - ' . $error_message;
  307.     } else {
  308.         $error = '#' . ((string) $error_number) . ' - ' . PMA_convert_display_charset($error_message);
  309.     }
  310.     return $error;
  311. }
  312.  
  313. function PMA_DBI_close($link = null)
  314. {
  315.     if (empty($link)) {
  316.         if (isset($GLOBALS['userlink'])) {
  317.             $link = $GLOBALS['userlink'];
  318.         } else {
  319.             return FALSE;
  320.         }
  321.     }
  322.     return @mysql_close($link);
  323. }
  324.  
  325. function PMA_DBI_num_rows($result) {
  326.     if (!is_bool($result)) {
  327.         return mysql_num_rows($result);
  328.     } else {
  329.         return 0;
  330.     }
  331. }
  332.  
  333. function PMA_DBI_insert_id($link = null)
  334. {
  335.     if (empty($link)) {
  336.         if (isset($GLOBALS['userlink'])) {
  337.             $link = $GLOBALS['userlink'];
  338.         } else {
  339.             return FALSE;
  340.         }
  341.     }
  342.     //$insert_id = mysql_insert_id($link);
  343.     // if the primary key is BIGINT we get an incorrect result
  344.     // (sometimes negative, sometimes positive)
  345.     // and in the present function we don't know if the PK is BIGINT
  346.     // so better play safe and use LAST_INSERT_ID()
  347.     //
  348.     // by the way, no problem with mysqli_insert_id()
  349.     return PMA_DBI_fetch_value('SELECT LAST_INSERT_ID();', 0, 0, $link);
  350. }
  351.  
  352. function PMA_DBI_affected_rows($link = null)
  353. {
  354.     if (empty($link)) {
  355.         if (isset($GLOBALS['userlink'])) {
  356.             $link = $GLOBALS['userlink'];
  357.         } else {
  358.             return FALSE;
  359.         }
  360.     }
  361.     return mysql_affected_rows($link);
  362. }
  363.  
  364. /**
  365.  * @todo add missing keys like in from mysqli_query (orgname, orgtable, flags, decimals)
  366.  */
  367. function PMA_DBI_get_fields_meta($result) {
  368.     $fields       = array();
  369.     $num_fields   = mysql_num_fields($result);
  370.     for ($i = 0; $i < $num_fields; $i++) {
  371.         $fields[] = PMA_convert_display_charset(mysql_fetch_field($result, $i));
  372.     }
  373.     return $fields;
  374. }
  375.  
  376. function PMA_DBI_num_fields($result) {
  377.     return mysql_num_fields($result);
  378. }
  379.  
  380. function PMA_DBI_field_len($result, $i) {
  381.     return mysql_field_len($result, $i);
  382. }
  383.  
  384. function PMA_DBI_field_name($result, $i) {
  385.     return mysql_field_name($result, $i);
  386. }
  387.  
  388. function PMA_DBI_field_flags($result, $i) {
  389.     return PMA_convert_display_charset(mysql_field_flags($result, $i));
  390. }
  391.  
  392. ?>
  393.